home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / Removing.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.4 KB  |  50 lines

  1. //: C21:Removing.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // The removing algorithms
  7. #include "PrintSequence.h"
  8. #include "Generators.h"
  9. #include <vector>
  10. #include <algorithm>
  11. #include <cctype>
  12. using namespace std;
  13.  
  14. struct IsUpper {
  15.   bool operator()(char c) {
  16.     return isupper(c);
  17.   }
  18. };
  19.  
  20. int main() {
  21.   vector<char> v(50);
  22.   generate(v.begin(), v.end(), CharGen());
  23.   print(v, "v", "");
  24.   // Create a set of the characters in v:
  25.   set<char> cs(v.begin(), v.end());
  26.   set<char>::iterator it = cs.begin();
  27.   vector<char>::iterator cit;
  28.   // Step through and remove everything:
  29.   while(it != cs.end()) {
  30.     cit = remove(v.begin(), v.end(), *it);
  31.     cout << *it << "[" << *cit << "] ";
  32.     print(v, "", "");
  33.     it++;
  34.   }
  35.   generate(v.begin(), v.end(), CharGen());
  36.   print(v, "v", "");
  37.   cit = remove_if(v.begin(), v.end(), IsUpper());
  38.   print(v.begin(), cit, "after remove_if", "");
  39.   // Copying versions are not shown for remove
  40.   // and remove_if.
  41.   sort(v.begin(), cit);
  42.   print(v.begin(), cit, "sorted", "");
  43.   vector<char> v2;
  44.   unique_copy(v.begin(), cit, back_inserter(v2));
  45.   print(v2, "unique_copy", "");
  46.   // Same behavior:
  47.   cit = unique(v.begin(), cit, equal_to<char>());
  48.   print(v.begin(), cit, "unique", "");
  49. } ///:~
  50.